package com.sh.util;

import org.springframework.stereotype.Component;
import sh.entity.Auth;

import java.util.ArrayList;
import java.util.List;

@Component
public class MenuUtil {

    /**
     * 菜单栏结构
     * @param authList
     * @return
     */
    public static List<Menu> getMenus(List<Auth> authList, Boolean isLogin){
        //创建一个菜单容器
        List<Menu> menuList=new ArrayList<>();
        //获取一级菜单
        for (Auth auth : authList) {
            if (auth==null) {
                continue;
            }
            //如果是菜单，等级为1  则是一级菜单
            if (auth.getIsMenu().equals(1)&&auth.getMenuLevel().equals(1)) {
                menuList.add(new Menu(auth.getAuthName(),auth.getAuthUrl(),auth.getMenuLevel(),auth.getAuthId(),auth.getPathName(),auth.getComponentPath(),auth.getMenuIcon(),null));
            }
        }
        //获取子级菜单
        for (Menu menu : menuList) {
            if (menu==null) {
                continue;
            }
            //二级菜单
            List<Menu> subSecondMenus = getSubMenus(menu, authList, 2,isLogin);
            for (Menu subSecondMenu : subSecondMenus) {
                if (subSecondMenu==null) {
                    continue;
                }
                //三级菜单
                List<Menu> subThirdMenus = getSubMenus(subSecondMenu, authList, 3,isLogin);
                for (Menu subThirdMenu : subThirdMenus) {
                    if (subThirdMenu==null) {
                        continue;
                    }
                    //四级菜单
                    List<Menu> subFourMenus = getSubMenus(subThirdMenu, authList, 4,isLogin);
//                    if (!isLogin) {
                        for (Menu subFourMenu : subFourMenus) {
                            if (subFourMenu==null) {
                                continue;
                            }
                            //五级菜单
                            List<Menu> subFiveMenus = getSubMenus(subFourMenu, authList, 5,isLogin);
                        }
//                    }

                }
            }
        }
        System.out.println(menuList);
        return menuList;
    }

    //获取子菜单和操作
    public static List<Menu> getSubMenus(Menu menu,List<Auth> authList,Integer level,Boolean isLogin){
        List<Menu> subMenu=new ArrayList<>();
        for (Auth auth : authList) {
            if (auth==null) {
                continue;
            }
            //如果不是菜单，是操作的话
            if (!isLogin) {
                if(auth.getIsMenu().equals(0)){
                    //是否是父级id
                    if(auth.getAuthParentId().equals(menu.getMenuId())){
                        subMenu.add(new Menu(auth.getAuthName(),auth.getAuthUrl(),auth.getMenuLevel(),auth.getAuthId(),auth.getPathName(),auth.getComponentPath(),auth.getMenuIcon(),null));
                    }
                }
            }
            //如果是菜单，等级是level  则是level级菜单
            if(auth.getIsMenu().equals(1)&&auth.getMenuLevel().equals(level)){
                //是否是父级id
                if(auth.getAuthParentId().equals(menu.getMenuId())){
                    subMenu.add(new Menu(auth.getAuthName(),auth.getAuthUrl(),auth.getMenuLevel(),auth.getAuthId(),auth.getPathName(),auth.getComponentPath(),auth.getMenuIcon(),null));
                }
            }
        }
        menu.setSubMenu(subMenu);
        return subMenu;
    }
}
